import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import * as moment from 'moment';
import Moment = moment.Moment;
// import DurationConstructor = moment.unitOfTime.DurationConstructor;

@Component({
  selector: 'main-page',
  templateUrl: 'main_page.html',
})
export class MainPage {
  public lastActiveTime: Moment;

  constructor(public navCtrl: NavController, public navParams: NavParams) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad MainPage');
  }


  public isActive(): boolean {
    return !this.isInactive();
  }

  public isInactive(): boolean {
    return true;
  }

  get timeInactiveString(): string {
    let now = moment();
    let duration = moment.duration(now.diff(this.lastActiveTime));
    let seconds = duration.asSeconds();
    let returnString = '';
    if (seconds < 60) {
      returnString = Math.round(seconds) + ' seconds';
    } else if (seconds < 3600) {
      returnString = this.prefixZero(duration.minutes()) + ':' + this.prefixZero(duration.seconds());
    } else if (seconds < 86400) {
      returnString = this.prefixZero(duration.hours()) + ':'
        + this.prefixZero(duration.minutes()) + ':' + this.prefixZero(duration.seconds());
    } else {
      returnString = duration.days() + ' days ' + this.prefixZero(duration.hours()) + ':'
        + this.prefixZero(duration.minutes()) + ':' + this.prefixZero(duration.seconds());
    }
    return returnString;
  }

  private prefixZero(num: number): string | number {
    if (num <= 9) {
      return '0' + num;
    }
    return num;
  }
}
